/*
 * Created by JFormDesigner on Sat Oct 23 15:22:20 CST 2021
 */

package view.info;

import library.controller.FrameListener;
import library.po.Book;
import library.po.Borrow;
import library.po.User;
import library.service.BookService;
import library.service.IdentityService;
import library.service.UserService;
import library.service.impl.BookServiceImpl;
import library.service.impl.BorrowServiceImpl;
import library.service.BorrowService;
import library.service.impl.IdentityServiceImpl;
import library.service.impl.UserServiceImpl;
import view.main.MainFrame;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author unknown
 */
public class OverviewDialog extends JDialog {
    public OverviewDialog(MainFrame owner,String title) {
        super(owner);
        listener = owner.getMainListener();
        listener.setOverviewDialog(this);
        this.setTitle(title);
        this.setVisible(true);
        initComponents();
    }
    public OverviewDialog(PersonalInfoDialog owner, String title ,User user){
        super(owner);
        listener = owner.getListener();
        listener.setOverviewDialog(this);
        this.setTitle(title);
        this.setVisible(true);
        this.user = userService.selectById(user.getUserId());
        initComponents();
    }


    private void initComponents() {
        scrollPane = new JScrollPane();
        adminViewBorrowPanel = new JPanel();
        adminBorrowButtonBar = new JPanel();
        borrowText = new JLabel();
        borrowSelectButton = new JButton();
        borrowSeparator = new JSeparator();
        readerViewBorrowPanel = new JPanel();
        readerBorrowButtonBar = new JPanel();
        reBorrowButton = new JButton();
        outputButton = new JButton();

        //======== this ========

        setSize(835, 515);
        setLocationRelativeTo(getOwner());
        //======== mouseClickMenu ========
        rightClickMenu = new JPopupMenu();
        copyItem = new JMenuItem();
        copyItem.setText("复制");
        copyItem.addActionListener(listener);
        copyItem.setActionCommand("CopyInfo");
        rightClickMenu.add(copyItem);

        //======== adminViewBorrowPanel ========
        if ("借阅记录".equals(this.getTitle())) {
            adminViewBorrowPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
            adminViewBorrowPanel.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.
                    swing.border.EmptyBorder(0, 0, 0, 0), "", javax.swing.border
                    .TitledBorder.CENTER, javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("D\u0069alog"
                    , java.awt.Font.BOLD, 12), java.awt.Color.red), adminViewBorrowPanel.getBorder
                    ()));
            adminViewBorrowPanel.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                @Override
                public void propertyChange(java.beans.PropertyChangeEvent e) {
                    if ("\u0062order".equals(e.getPropertyName())) throw new RuntimeException
                            ();
                }
            });
            adminViewBorrowPanel.setLayout(null);

            //======== adminBorrowButtonBar ========
            {
                adminBorrowButtonBar.setBorder(new EmptyBorder(12, 5, 0, 5));
                adminBorrowButtonBar.setLayout(null);

                //---- borrowText ----
                borrowText.setText("借阅记录不可随意添加/删除/修改，但可复制和导出Excel文件。");
                borrowText.setFont(new Font("幼圆", borrowText.getFont().getStyle() & ~Font.BOLD, borrowText.getFont().getSize() + 6));
                adminBorrowButtonBar.add(borrowText);
                borrowText.setBounds(0, 0, 545, 40);

                //---- outputButton----
                outputButton.setText("\u5bfc\u51fa");
                outputButton.addActionListener(listener);
                outputButton.setActionCommand("Output");
                outputButton.setFont(new Font("幼圆", outputButton.getFont().getStyle() & ~Font.BOLD, outputButton.getFont().getSize() + 8));
                adminBorrowButtonBar.add(outputButton);
                outputButton.setBounds(680, 0, 105, 40);

                //---- borrowSelectButton ----
                borrowSelectButton.setText("查询");
                borrowSelectButton.setFont(new Font("幼圆", borrowSelectButton.getFont().getStyle() & ~Font.BOLD, borrowSelectButton.getFont().getSize() + 8));
                borrowSelectButton.addActionListener(listener);
                borrowSelectButton.setActionCommand("SelectBorrowDialog");
                adminBorrowButtonBar.add(borrowSelectButton);
                borrowSelectButton.setBounds(520, 0, 100, 40);


                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for (int i = 0; i < adminBorrowButtonBar.getComponentCount(); i++) {
                        Rectangle bounds = adminBorrowButtonBar.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = adminBorrowButtonBar.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    adminBorrowButtonBar.setMinimumSize(preferredSize);
                    adminBorrowButtonBar.setPreferredSize(preferredSize);
                }
            }
            adminViewBorrowPanel.add(adminBorrowButtonBar);
            adminBorrowButtonBar.setBounds(12, 12, 799, adminBorrowButtonBar.getPreferredSize().height);

            //======== scrollPane ========
            {
                table = this.getAdminBorrowTable(borrowService.selectAllBorrow());
                scrollPane.setViewportView(table);
            }
            adminViewBorrowPanel.add(scrollPane);
            scrollPane.setBounds(12, 65, 799, 396);

            //---- borrowSeparator ----
            borrowSeparator.setBackground(Color.black);
            borrowSeparator.setForeground(Color.black);
            adminViewBorrowPanel.add(borrowSeparator);
            borrowSeparator.setBounds(10, 60, 799, borrowSeparator.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < adminViewBorrowPanel.getComponentCount(); i++) {
                    Rectangle bounds = adminViewBorrowPanel.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = adminViewBorrowPanel.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                adminViewBorrowPanel.setMinimumSize(preferredSize);
                adminViewBorrowPanel.setPreferredSize(preferredSize);
            }
        }
        this.panel = adminViewBorrowPanel;
        this.add(panel);
        //======== readerViewBorrowPanel ========
        if ("已借图书记录".equals(this.getTitle())) {
            readerViewBorrowPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
            readerViewBorrowPanel.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing.border
                    .EmptyBorder(0, 0, 0, 0), "", javax.swing.border.TitledBorder.CENTER, javax
                    .swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialo\u0067", java.awt.Font.BOLD,
                    12), java.awt.Color.red), readerViewBorrowPanel.getBorder()));
            readerViewBorrowPanel.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                @Override
                public void propertyChange(java.beans.PropertyChangeEvent e) {
                    if ("borde\u0072".equals(e.getPropertyName())) throw new RuntimeException();
                }
            });
            readerViewBorrowPanel.setLayout(null);

            //======== readerBorrowButtonBar ========
            {
                readerBorrowButtonBar.setBorder(new EmptyBorder(12, 5, 0, 5));
                readerBorrowButtonBar.setLayout(null);

                //---- returnButton
                returnButton = new JButton("归还");
                returnButton.setFont(new Font("幼圆", reBorrowButton.getFont().getStyle() & ~Font.BOLD, reBorrowButton.getFont().getSize() + 8));
                returnButton.addActionListener(listener);
                returnButton.setActionCommand("ReturnBook");
                readerBorrowButtonBar.add(returnButton);
                returnButton.setBounds(360,0,105,40);

                //---- reBorrowButton ----
                reBorrowButton.setText("\u7eed\u501f");
                reBorrowButton.setFont(new Font("幼圆", reBorrowButton.getFont().getStyle() & ~Font.BOLD, reBorrowButton.getFont().getSize() + 8));
                reBorrowButton.addActionListener(listener);
                reBorrowButton.setActionCommand("ReBorrow");
                readerBorrowButtonBar.add(reBorrowButton);
                reBorrowButton.setBounds(680, 0, 105, 40);

                //---- borrowSelectButton ----
                borrowSelectButton.setText("查询");
                borrowSelectButton.setFont(new Font("幼圆", borrowSelectButton.getFont().getStyle() & ~Font.BOLD, borrowSelectButton.getFont().getSize() + 8));
                borrowSelectButton.addActionListener(listener);
                borrowSelectButton.setActionCommand("SelectBorrowDialog");
                readerBorrowButtonBar.add(borrowSelectButton);
                borrowSelectButton.setBounds(520, 0, 100, 40);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for (int i = 0; i < readerBorrowButtonBar.getComponentCount(); i++) {
                        Rectangle bounds = readerBorrowButtonBar.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = readerBorrowButtonBar.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    readerBorrowButtonBar.setMinimumSize(preferredSize);
                    readerBorrowButtonBar.setPreferredSize(preferredSize);
                }
            }
            readerViewBorrowPanel.add(readerBorrowButtonBar);
            readerBorrowButtonBar.setBounds(12, 12, 799, readerBorrowButtonBar.getPreferredSize().height);

            //======== scrollPane ========
            {
                table = this.getReaderBorrowTable(borrowService.selectByUserId(user.getUserId()));
                scrollPane.setViewportView(table);
            }
            readerViewBorrowPanel.add(scrollPane);
            scrollPane.setBounds(12, 65, 799, 396);

            //---- borrowSeparator ----
            borrowSeparator.setBackground(Color.black);
            borrowSeparator.setForeground(Color.black);
            readerViewBorrowPanel.add(borrowSeparator);
            borrowSeparator.setBounds(10, 60, 799, borrowSeparator.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < readerViewBorrowPanel.getComponentCount(); i++) {
                    Rectangle bounds = readerViewBorrowPanel.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = readerViewBorrowPanel.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                readerViewBorrowPanel.setMinimumSize(preferredSize);
                readerViewBorrowPanel.setPreferredSize(preferredSize);
            }
            this.panel = readerViewBorrowPanel;
            this.add(panel);
        }
    }




    public JTable getReaderBorrowTable(List<Borrow> borrowList){
        String titles[] = {"ISBN", "书名", "借阅时间", "归还时间", "应还时间", "状态", "剩余续借次数" };
        Object data[][] = new Object[borrowList.size()][7];
        Iterator<Borrow> iter = borrowList.iterator();
        int i = 0;
        while (iter.hasNext()) {
            Borrow borrow = iter.next();
            data[i][0] = borrow.getBookIsbn();
            data[i][1] = borrow.getBookName();
            data[i][2] = borrow.getBorrowTime();
            data[i][3] = borrow.getReturnTime();
            data[i][4] = borrow.getCorrectReturnTime();
            data[i][5] = borrow.getBorrowState();
            data[i][6] = borrow.getRenewTimes();
            i++;
        }
        DefaultTableModel tableModel = new DefaultTableModel(data,titles);
        JTable table = new JTable(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(900, 600));
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
        table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
        table.getTableHeader().setPreferredSize(new Dimension(1, 50)); //表头高度
        table.setRowHeight(table.getRowHeight()+30); //表体高度
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
        table.repaint();
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(final MouseEvent e) {
                int mods=e.getModifiers();
                table.add(rightClickMenu);
                //鼠标右键
                if((mods& InputEvent.BUTTON3_MASK)!=0){
                    //弹出菜单
                    rightClickMenu.show(table, e.getX(), e.getY());
                }
                selRow = table.getSelectedRow();  //得到选中的行
            }
        });
        return table;
    }
    public JTable getAdminBorrowTable(List<Borrow> borrowList){
        String titles[] = {"用户","ISBN", "书名", "借阅时间", "归还时间", "应还时间", "状态", "剩余续借次数" };
        Object data[][] = new Object[borrowList.size()][8];
        Iterator<Borrow> iter = borrowList.iterator();
        int i = 0;
        while (iter.hasNext()) {
            Borrow borrow = iter.next();
            data[i][0] = borrow.getUserId();
            data[i][1] = borrow.getBookIsbn();
            data[i][2] = borrow.getBookName();
            data[i][3] = borrow.getBorrowTime();
            data[i][4] = borrow.getReturnTime();
            data[i][5] = borrow.getCorrectReturnTime();
            data[i][6] = borrow.getBorrowState();
            data[i][7] = borrow.getRenewTimes();
            i++;
        }
        DefaultTableModel tableModel = new DefaultTableModel(data,titles);
        JTable table = new JTable(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(900, 600));
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
        table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
        table.getTableHeader().setPreferredSize(new Dimension(1, 50)); //表头高度
        table.setRowHeight(table.getRowHeight()+30); //表体高度
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
        table.repaint();
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(final MouseEvent e) {
                int mods=e.getModifiers();
                table.add(rightClickMenu);
                //鼠标右键
                if((mods& InputEvent.BUTTON3_MASK)!=0){
                    //弹出菜单
                    rightClickMenu.show(table, e.getX(), e.getY());
                }
                selRow = table.getSelectedRow();  //得到选中的行
            }
        });
        return table;
    }
    /*获取选中的列*/
    public int getSelRow(){
        return this.selRow;
    }
    /*刷新borrow表*/
    /*获取该对话框的监听器*/
    public void borrowRefresh(List<Borrow> borrowList) {
        if (this.getTitle().equals("已借图书记录")){
            this.remove(panel);
            panel.remove(scrollPane);
            table = this.getReaderBorrowTable(borrowList);
            scrollPane.setViewportView(table);
            panel.add(scrollPane);
            this.add(panel,BorderLayout.CENTER);
            this.validate();
        }
        else if (this.getTitle().equals("借阅记录")){
            this.remove(panel);
            panel.remove(scrollPane);
            table = this.getAdminBorrowTable(borrowList);
            scrollPane.setViewportView(table);
            panel.add(scrollPane);
            this.add(panel,BorderLayout.CENTER);
            this.validate();
        }
    }
    public FrameListener getListener(){
        return this.listener;
    }
    //整个界面公用
    private JPopupMenu rightClickMenu;
    private JMenuItem copyItem;
    private JScrollPane scrollPane;
    private JButton outputButton;
    private JTable table;
    private FrameListener listener;
    private User user;
    private int selRow = -1;
    private BorrowService borrowService = new BorrowServiceImpl();
    private BookService bookService = new BookServiceImpl();
    private UserService userService = new UserServiceImpl();
    private IdentityService identityService = new IdentityServiceImpl();
    //borrow
    private JPanel adminViewBorrowPanel;
    private JPanel adminBorrowButtonBar;
    private JLabel borrowText;
    private JButton borrowSelectButton;
    private JSeparator borrowSeparator;

    private JPanel readerViewBorrowPanel;
    private JPanel readerBorrowButtonBar;
    private JButton reBorrowButton;
    private JButton returnButton;

    private JPanel panel;
}
